package com.daoeping.action;

import java.util.ArrayList;
import java.util.List;
import com.daoeping.data.Usr_user;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.service.Usr_userService;
import com.daoeping.util.ConverntJsonData;
import com.daoeping.util.Md5Password;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Usr_userAction extends ActionSupport{
	private Usr_userService usr_userService;
	private JsonData jsonData;
	private List<Usr_user> usr_userList;
	private Usr_user usr_user;
	private TableData tableData;
	private Page page;
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public TableData getTableData() {
		return tableData;
	}

	public void setTableData(TableData tableData) {
		this.tableData = tableData;
	}

	public Usr_user getUsr_user() {
		return usr_user;
	}

	public void setUsr_user(Usr_user usr_user) {
		this.usr_user = usr_user;
	}

	public List<Usr_user> getUsr_userList() {
		return usr_userList;
	}

	public void setUsr_userList(List<Usr_user> usr_userList) {
		this.usr_userList = usr_userList;
	}

	public JsonData getJsonData() {
		return jsonData;
	}

	public void setJsonData(JsonData jsonData) {
		this.jsonData = jsonData;
	}

	public Usr_userService getUsr_userService() {
		return usr_userService;
	}

	public void setUsr_userService(Usr_userService usr_userService) {
		this.usr_userService = usr_userService;
	}


	
	public String findUsr_userByNameAndPass(){
		List<String> list = new ArrayList<String>();
		System.out.println(usr_user.getPassword()+"===");
		if(usr_userService.findUsr_userByNameAndPass(usr_user)&&verifyCode(jsonData)){
			list.add("sucess_login");
			jsonData = new JsonData();
			jsonData.setJsonData(new ConverntJsonData().getJsonData(list).toString());
			return "findUsr_userByNameAndPass";
		}else{
			list.add("sucess_failed");
			jsonData = new JsonData();
			jsonData.setJsonData(new ConverntJsonData().getJsonData(list).toString());
			return "failed";
		}
	}
	
	public String findUsr_userByName(){
		return "findUsr_userByName";
	}
	
	public String addUsr_user(){
		ActionContext.getContext().getSession().put("user", usr_user);
		if(verifyCode(jsonData)){
			if(!usr_userService.findUsr_userByName(usr_user)){
				System.out.println(usr_user.getPassword());
				usr_user.setPassword(new Md5Password().setmd5Pass(usr_user.getPassword()));
				if(usr_userService.addUsr_user(usr_user)){
					ActionContext.getContext().getSession().put("user", usr_user);
					return "addUsr_user";
				}else{
					return "failed";
				}
			}else{
				return "failed";
			}
		}else{
			return "failed";
		}
	}
	
	public String findAllUsr_userNotFriendByPage(){
		Usr_user usr_user = (Usr_user)ActionContext.getContext().getSession().get("user");
		// fuzzy matching
		System.out.println(jsonData.getJsonData());
		usr_user.setUsername(jsonData.getJsonData());
		jsonData = usr_userService.findAllUsr_userNotFriendByPage(page, tableData,usr_user);
		return "findAllUsr_userNotFriendByPage";
	}
	
	public String delUsr_userBySesson(){
		System.out.println(ActionContext.getContext().getSession().get("user")+":before");
		ActionContext.getContext().getSession().remove("user");
		System.out.println(ActionContext.getContext().getSession().get("user")+":after");
		List<String> list = new ArrayList<String>();
		list.add("sucess_logout");
		jsonData = new JsonData();
		jsonData.setJsonData(new ConverntJsonData().getJsonData(list)+"");
		return "delUsr_userBySesson";
	}
	
	
	public boolean verifyCode(JsonData jsonData){
		String verifycode = ActionContext.getContext().getSession().get("verifycode").toString();
		if(!"".equals(verifycode)&&verifycode.equals(jsonData.getJsonData())){
			return true;
		}else{
			return false;
		}
	}
}
